package com.collectplus.express.parcel;

import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.logic.DialogMgr;
import com.collectplus.express.model.ParcelReceiveReachedStationBean;
import com.collectplus.express.parcel.fragment.BaseReceiverParcelFragment;
import com.collectplus.express.view.CircleImageView;

import java.text.MessageFormat;
import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.GlideUtil;
import droid.frame.utils.lang.Str;

/**
 * 收到的包裹通用adapter
 *
 * @author kaka<br>
 *         2017-1-9下午4:33:22
 */
public class ParcelReceiveAdapter extends BaseAdapterExt<ParcelReceiveReachedStationBean> {

    private String stationParcelStr = "<font color=#404040>暂存编码：{0}(请在</font><font color=#FF001F>{1}号</font><font color=#404040>货架上查找该包裹)</font>";
    private BaseReceiverParcelFragment fragment;

    public ParcelReceiveAdapter(ArrayList<ParcelReceiveReachedStationBean> items, BaseReceiverParcelFragment fragment) {
        super(items, fragment.getActivityContext());
        this.fragment = fragment;
        setEmptyView(R.layout.parcel_receive_empty);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.parcel_receiver_reached_station_item, parent, false);
            holder.topLineV = convertView.findViewById(R.id.top_line_v);
            holder.stationParcelNumTV = (TextView) convertView.findViewById(R.id.station_parcel_num_tv);
            holder.stationHeadImageIV = (CircleImageView) convertView.findViewById(R.id.station_headimage_iv);
            holder.stationNameTV = (TextView) convertView.findViewById(R.id.station_name_tv);
            holder.stationPhoneTV = (TextView) convertView.findViewById(R.id.station_phone_tv);
            holder.stationAddressTV = (TextView) convertView.findViewById(R.id.station_address_tv);
            holder.parcelLayoutLL = (LinearLayout) convertView.findViewById(R.id.parcel_list_layout);
            holder.pickUpParcelsTV = (TextView) convertView.findViewById(R.id.pick_up_parcels_tv);
            holder.bottomLineV = convertView.findViewById(R.id.bottom_line_v);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ParcelReceiveReachedStationBean item = getItem(position);
        holder.stationParcelNumTV.setText("有" + item.getParcels().size() + "个包裹被投递至该驿站，请前往取件！");
        GlideUtil.loadImage(context, item.getHeadImage(), 0, holder.stationHeadImageIV);
        holder.stationNameTV.setText(item.getName());
        holder.stationPhoneTV.setText("联系电话：" + item.getPhone());
        holder.stationAddressTV.setText("地址：" + item.getAddress());
        holder.parcelLayoutLL.removeAllViews();
        for (int i = 0; i < item.getParcels().size(); i++) {
            ParcelReceiveReachedStationBean.ParcelsBean parcelsBean = item.getParcels().get(i);
            View layout = LayoutInflater.from(context).inflate(R.layout.parcel_receiver_item, null);
            TextView parcelNumTV = (TextView) layout.findViewById(R.id.parcel_num_tv);
            ImageView expressLogoIV = (ImageView) layout.findViewById(R.id.express_logo);
            TextView expressCodeTV = (TextView) layout.findViewById(R.id.express_code_tv);
            TextView recipientTV = (TextView) layout.findViewById(R.id.parcel_sender_name_tv);
            TextView receiverPhoneTV = (TextView) layout.findViewById(R.id.parcel_contract_phone_tv);
            TextView stagingCodeTV = (TextView) layout.findViewById(R.id.staging_code_tv);
            TextView deliveryTimeTV = (TextView) layout.findViewById(R.id.delivery_time_tv);
            ImageView parcelSizeIV = (ImageView) layout.findViewById(R.id.parcel_size);
            View divdeLineV = layout.findViewById(R.id.divde_line_v);

            parcelNumTV.setText((i + 1) + "");
            GlideUtil.loadImage(context, parcelsBean.getExpressCompanyUrl(), 0, expressLogoIV);
            if (Str.isNotEmpty(parcelsBean.getExpressNumber())) {
                expressCodeTV.setText(parcelsBean.getExpressCompanyName() + "：" + parcelsBean.getExpressNumber());
            } else {
                expressCodeTV.setText(parcelsBean.getExpressCompanyName());
            }
            recipientTV.setText("收件人：" + parcelsBean.getReceiverName());
            receiverPhoneTV.setText("联系电话：" + parcelsBean.getReceiverPhone());
            stagingCodeTV.setText(Html.fromHtml(MessageFormat.format(stationParcelStr, parcelsBean.getStorageCode(), parcelsBean.getStorageRack())));
            stagingCodeTV.getPaint().setFakeBoldText(true);
            deliveryTimeTV.setText(parcelsBean.getCreateTime() + "  投递至该驿站");
            if (parcelsBean.isBig()) {
                parcelSizeIV.setImageResource(R.drawable.take_parcel_big_icon);
            } else {
                parcelSizeIV.setImageResource(R.drawable.take_parcel_small_icon);
            }
            if (i == item.getParcels().size() - 1) {
                divdeLineV.setVisibility(View.INVISIBLE);
            } else {
                divdeLineV.setVisibility(View.VISIBLE);
            }
            holder.parcelLayoutLL.addView(layout);
        }
        holder.pickUpParcelsTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item != null) {
                    DialogMgr.confirmReceiveParcelTake(fragment, item, position);
                }
            }
        });
        if (position == 0) {
            if (getItemsSize() == 1) {
                holder.bottomLineV.setVisibility(View.INVISIBLE);
            } else {
                holder.bottomLineV.setVisibility(View.VISIBLE);
            }
            holder.topLineV.setVisibility(View.GONE);
        } else {
            holder.topLineV.setVisibility(View.VISIBLE);
            if (getItemsSize() - 1 == position) {
                holder.bottomLineV.setVisibility(View.INVISIBLE);
            } else {
                holder.bottomLineV.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }

    class ViewHolder {
        View topLineV;
        TextView stationParcelNumTV;
        CircleImageView stationHeadImageIV;
        TextView stationNameTV;
        TextView stationPhoneTV;
        TextView stationAddressTV;
        LinearLayout parcelLayoutLL;
        TextView pickUpParcelsTV;
        View bottomLineV;
    }
}
